<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2023/6/27
 * Time: 16:27
 */

namespace App\Rep;


use App\Models\ModelFields;
use App\Models\Models;
use App\Models\Product;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class ModelRep
{
    /**
     * 获取模型列表
     * @param $params
     */
    public function getModelListRep($params){
        $list = Models::Search($params)->orderBy('created_at','desc')->paginate($params['per_page']??10);
        foreach($list as $k=>$v){
            //$str = mb_unserialize($v->setting);
            $list[$k]['setting'] = json_decode($v->setting,true);
        }
        return $list;
    }

    /**
     * 获取模型下拉列表
     * @param $params
     * @return mixed
     */
    public function getModelDropDownListRep($params){
        return Models::Search($params)->orderBy('created_at','desc')->select('modelid','modelname')->get();
    }

    /**
     * 添加模型
     * @param $params
     */
    public function addModelRep($params){
        //p($params);
        $json = file_get_contents(public_path('json').'/order.json');
        $str = str_replace(' ','',str_replace(PHP_EOL, '', $json));
        //p($str);
        if(isset($params['id']) && !empty($params['id'])){
            $model = Models::where('id',$params['id'])->first();
            $model->modelname = $params['modelname'];
            $model->setting = $str;
        }else{
            $model = Models::where('tablename',$params['tablename'])->first();
            if($model){
                return ['code'=>2001,'data'=>[],'msg'=>'数据表已存在'];
            }
            if(!Schema::connection('mysql')->hasTable($params['tablename'])){
                Schema::connection('mysql')->create($params['tablename'], function ($table) {
                    $table->engine = 'InnoDB';
                    $table->increments('id');
                    $table->Integer('order_id');
                });
            }else{
                return ['code'=>2001,'data'=>[],'msg'=>'数据表已存在'];
            }
            $model = new Models();
            $model->modelname = $params['modelname'];
            $model->tablename = $params['tablename'];
            $model->setting = $str;


        }
        $model->save();
        return ['code'=>0,'data'=>[],'msg'=>'success'];
    }

    /**
     * 添加字段
     * formtype
     * @param $params
     */
    public function addModelFieldRep($params){
        $model = Models::where('modelid',$params['modelid'])->first();
        $where = [
            'field'=>$params['field'],
            'modelid'=>$params['modelid']
        ];
        $obj = ModelFields::updateOrCreate($where,[
            'field'=>$params['field'],
            'modelid'=>$params['modelid'],
            'name'=>$params['name'],
            'isshow'=>$params['isshow'],
            'formtype'=>$params['formtype'],
            'listorder'=>$params['listorder'],
            'required'=>$params['required']
        ]);
        $model_field = ModelFields::where('fieldid',$obj->fieldid)->first();
        $this->up($model->tablename,$model_field->formtype,$model_field->field,$model_field->name);
        return $model;
    }

    public function up($tableName,$formtype,$field,$name)
    {
        if(Schema::hasColumn($tableName,$field)){
            Schema::table($tableName, function (Blueprint $table)use($formtype,$field,$name) {
                switch($formtype) {
                    case 'input':
                        $table->string($field)->comment($name)->change();
                        break;
                    case 'textarea':
                        $table->longText($field)->comment($name)->change();
                        break;
                    case 'radio':
                        $table->integer($field)->comment($name)->change();
                        break;
                    case 'select':
                        $table->integer($field)->comment($name)->change();
                        break;
                    case 'file':
                        $table->integer($field)->comment($name)->change();
                        break;
                    case 'checkbox':
                        $table->string($field)->comment($name)->change();
                        break;
                }
            });
        }else{
            Schema::table($tableName, function (Blueprint $table)use($formtype,$field,$name) {
                switch($formtype) {
                    case 'input':
                        $table->string($field)->comment($name);
                        break;
                    case 'textarea':
                        $table->longText($field)->comment($name);
                        break;
                    case 'radio':
                        $table->integer($field)->comment($name);
                        break;
                    case 'select':
                        $table->integer($field)->comment($name);
                        break;
                    case 'file':
                        $table->integer($field)->comment($name);
                        break;
                    case 'checkbox':
                        $table->string($field)->comment($name);
                        break;
                }
            });
        }

    }

    /**
     * 获取字段列表
     * @param $params
     */
    public function getModelFieldListRep($params){
        $model = Models::where('modelid',$params['modelid'])->first();
        $setting = json_decode($model->setting,true);
        //p($setting);
        $new = ModelFields::where('modelid',$params['modelid'])->get();
        return ['code'=>0,'data'=>['defaule'=>$setting['default'],'new'=>$new],'msg'=>'success'];
    }

    /**
     * 默认字段显示设置
     * @param $params
     */
    public function updateDefauleFieldShowRep($params){
        $model = Models::where('modelid',$params['modelid'])->first();
        $setting = json_decode($model->setting,true);
        $setting['default'][$params['field']]['show'] = $params['show'];
        $model->setting = json_encode($setting);
        $model->save();
        return $model;
    }

    /**
     * 删除字段
     * @param $params
     */
    public function delModelFieldRep($params){
        $model = Models::where('modelid',$params['modelid'])->first();
        $obj = ModelFields::where('fieldid',$params['fieldid'])->first();
        $field = $obj->field;
        Schema::table($model->tablename, function (Blueprint $table)use($field) {
            $table->dropColumn($field);
        });
        $obj->delete();
        return ['code'=>0,'data'=>$obj,'msg'=>'success'];
    }

    /**
     * 获取产品字段
     */
    public function getProductFieldListRep($params){
        $product = Product::where('id',$params['product_id'])->first();
        $model = Models::where('modelid',$product->modelid)->first();
        $model_field = ModelFields::where('modelid',$product->modelid)->orderBy('listorder','asc')->get();
        $setting = json_decode($model->setting,true);
        return ['default'=>$setting['default'],'new'=>$model_field];
    }






}